//
//  XYZPhoto.m
//  demo6_PhotoRiver
//
//  Created by BOBO on 15/3/23.
//  Copyright (c) 2015年 BobooO. All rights reserved.
//

#import "XYZPhoto.h"

@implementation XYZPhoto

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor blackColor];
//        self.drawView.backgroundColor = [UIColor redColor];
        self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
//        self.drawView = [[XYZDrawView alloc]initWithFrame:self.bounds];
        self.imageView.backgroundColor = [UIColor clearColor];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//        self.drawView.contentMode = UIViewContentModeScaleAspectFit;
//        [self addSubview:self.drawView];
        [self addSubview:self.imageView];
//        CATransition *animation = [CATransition animation];
//        animation.duration = 1;
//        animation.timingFunction = UIViewAnimationCurveEaseInOut;
//        // 改编动画样式
//        animation.type = kCATransitionPush;
//        // animation.type = @"";
//        animation.subtype = kCATransitionFromLeft;
//        [self.layer addAnimation:animation forKey:@"animation"];
//        
//        self.transform = CGAffineTransformMakeTranslation(0, 100);
        
        NSTimer *timer = [NSTimer timerWithTimeInterval:1/10 target:self selector:@selector(movePhotos) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:@"NSDefaultRunLoopMode"];
        
        self.layer.borderColor = [[UIColor clearColor] CGColor];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage)];
        [self addGestureRecognizer:tap];
        
//        UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipImage)];
//        [swip setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
//        [self addGestureRecognizer:swip];

        
    }
    return self;
}

- (void)tapImage {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        if (self.state == XYZPhotoStateNormal) {
            self.oldFrame = self.frame;
            self.oldAlpha = self.alpha;
            self.oldSpeed = self.speed;
            self.frame = CGRectMake(0, 0, self.superview.bounds.size.width , self.superview.bounds.size.height);
            self.imageView.frame = self.bounds;
//            self.drawView.frame = self.bounds;
            [self scaleImage:self.imageView.image toScale:5];
          
//            CGSize itemSize = CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height);
//            UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
//            CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
//            [self.imageView.image drawInRect:imageRect];
//            
//
//            self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//            UIGraphicsEndImageContext();
//            [self.imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(300, 300, 300, 300)];
            
            [self.superview bringSubviewToFront:self];
            self.speed = 0;
            self.alpha = 1;
            self.state = XYZPhotoStateBig;
            
        } else if (self.state == XYZPhotoStateBig) {
            self.frame = self.oldFrame;
            self.alpha = self.oldAlpha;
            self.speed = self.oldSpeed;
            self.imageView.frame = self.bounds;
//            self.drawView.frame = self.bounds;
            self.state = XYZPhotoStateNormal;
        }
    }];
   
}

- (void)swipImage {
    
    if (self.state == XYZPhotoStateBig) {
        [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        self.state = XYZPhotoStateDraw;
    } else if (self.state == XYZPhotoStateDraw){
        [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        self.state = XYZPhotoStateBig;
    }
}


- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize

{

    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
    
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    
 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
                                
UIGraphicsEndImageContext();
   return scaledImage;
                              
}


- (void)updateImage:(UIImage *)image {
    UIImageView *tempimage = [[UIImageView alloc] initWithFrame:self.frame];
    tempimage.image = image;
    
    self.imageView.image = tempimage.image;
}

- (void)hahahImage:(NSString *)imageURL {
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]];
    
}

- (void)addmyImage:(CGImageRef)image {
    self.imageView.image = [UIImage imageWithCGImage:image];
}


- (void)setImageAlphaAndSpeedAndSize:(float)alpha {
    self.alpha = alpha;
    self.speed = alpha;
    self.transform = CGAffineTransformScale(self.transform, alpha, alpha);
}

- (void)movePhotos {
    
    self.center = CGPointMake(self.center.x + self.speed, self.center.y);
    if (self.center.x > self.superview.bounds.size.width + self.frame.size.width/2) {
        self.center = CGPointMake(-self.frame.size.width/2, arc4random()%(int)(self.superview.bounds.size.height - self.bounds.size.height) + self.bounds.size.height/2);
    }
}



@end
